package com.share.tool.m2d.drds;

/**
 * @author syq
 * @data 2020/1/7
 */
public class MainDrds {
    private static String sqlInsert = "INSERT INTO `bbt_jd_member` (`id`, `seller_nick`, `pin`, `mobile`, `fullname`, `gender`, `grade`, `active_count`, `trade_count`, `trade_amount`, `avg_price`, `first_trade_time`, `year_trade_count`, `year_trade_amount`, `year_avg_price`, `trade_count18`, `trade_amount18`, `avg_price18`, `trade_count19`, `trade_amount19`, `avg_price19`, `last_trade_price`, `last_trade_time`, `cancel_trade_count`, `province`, `last_province`, `order_source`, `last_order_source`, `is_filter`, `groups`, `create_time`, `modify_time`) VALUES (106271119,'995529','wdatzepqplttod','18755408899','程引露',0,1,0,1,118,118,'2019-05-12 09:26:08',0,0,0,0,0,0,1,118,118,118,'2019-05-12 09:26:08',0,'江苏',NULL,'微信订单',NULL,0,'','2019-06-26 14:35:00','2019-06-26 14:35:00');";

    public static void main(String[] args) {
        /*java -jar bbt-data.jar sql skipColounIndexs skipDuplicateIndexs*/
        if (args.length != 3) {
            System.out.println("参数个数错误-->java -jar bbt-data.jar sql skipColounIndexs skipDuplicateIndexs");
            return;
        }
        String sql = args[0];
        String skipColounIndexs = args[1]+','; /*0,*/
        String skipDuplicateIndexs = args[2]+',';/*0,1,3,*/

//        String sql = sqlInset;
//        String skipColounIndexs = "0,";
//        String skipDuplicateIndexs = "0,1,3,";
        String reSql = strongInsert(sql, skipColounIndexs,skipDuplicateIndexs);
        System.out.println(reSql);
    }

    private static String strongInsert(String sql, String skipColounIndexs,String skipDuplicateIndexs) {
        int startColoun = sql.indexOf("(");
        int endColoun = sql.indexOf(")", startColoun);
        int startValue = sql.indexOf("(", endColoun);
        int endValue = sql.lastIndexOf(")");
        final int startLen = 5;
        if (startColoun < startLen || startColoun >= endColoun || endColoun >= startValue || startValue > endValue) {
            return null;
        }

        String beginStr = sql.substring(0, startColoun);
        String colounStr = sql.substring(startColoun + 1, endColoun);
        String valueStr = sql.substring(startValue + 1, endValue);
        if (colounStr == null || valueStr == null || beginStr.length() < startLen) {
            return null;
        }

        String[] arrColoun = colounStr.split(",");
        String[] arrValue = valueStr.split(",");
        int len = arrColoun.length;
        if (arrColoun.length != arrValue.length || len < 1) {
            return null;
        }

        //insert
        StringBuffer reSb = new StringBuffer(beginStr);
        for (int i = 0; i < len; i++) {
            if (i == 0) {
                reSb.append("(");
            }

            if (skipColounIndexs.contains(i+",")) {
                continue;
            }
            reSb.append(arrColoun[i]+",");

            if (i == len - 1) {
                int index = reSb.lastIndexOf(",");
                if(index>0){
                    reSb.setCharAt(index,')');
                }
            }
        }

        //value
        reSb.append("values");
        for (int i = 0; i < len; i++) {
            if (i == 0) {
                reSb.append("(");
            }

            if (skipColounIndexs.contains(i+",")) {
                continue;
            }
            reSb.append(arrValue[i]+",");

            if (i == len - 1) {
                int index = reSb.lastIndexOf(",");
                if(index>0){
                    reSb.setCharAt(index,')');
                }
            }
        }

        //dump
        reSb.append(" ON DUPLICATE KEY UPDATE ");
        for (int i = 0; i < len; i++) {
            if (skipDuplicateIndexs.contains(i+",")) {
                continue;
            }
            reSb.append(arrColoun[i]+"="+arrValue[i]+",");

            if (i == len - 1) {
                int index = reSb.lastIndexOf(",");
                if(index>0){
                    reSb.setCharAt(index,';');
                }
            }
        }

        return  reSb.toString();
    }
}
